package com.shop.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shop.entity.Goods;
import com.shop.entity.Page;
import com.shop.service.GoodsService;
import com.shop.service.impl.GoodsServiceImpl;

@WebServlet("/getGoods.do")
public class GetGoodsServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GoodsService goodsService = new GoodsServiceImpl();
		Page page = null;
		int pageSize;
		int pageNum;
		int totalRow = goodsService. getAllGoodsRows();
		try {
			pageSize = Integer.parseInt(req.getParameter("pageSize"));
		} catch (Exception e) {
			pageSize = 5;
		} 
		try {
			pageNum = Integer.parseInt(req.getParameter("pageNum"));
		} catch (Exception e) {
			pageNum = 1;
		} 
		List<Goods> goodsList = goodsService.getAllGoods(pageSize,pageNum);
		page = new Page(goodsList,pageNum,pageSize,totalRow);
		
		req.setAttribute("page", page);
		req.getRequestDispatcher("goods.jsp").forward(req, resp);
	}
}
